var ac;

function ApplicationContext() {
	this.self = this;
	
	this.snows = [];
	this.gravity;
	this.winds = [];
	this.windCount;
	this.windMapBySnow = [];
	
	this.windowMaxX;
	this.windowMaxY;
	
	this.parentHtmlObject = document.getElementsByTagName("body")[0];
	
	this.init = function() {
		this._initSelf();
		
		this._initWinds();
		this._initGravity();
		this._initSnows();
		
		setInterval(function() { ac.run(ac); }, 45);
	//	ac.run(ac);
	};
	
	this.run = function(self) {
		for (var i = 0, length = self.snows.length; i < length; i++) {
			var key = "" + i;
			
		//	console.log("snow " + i + " is : " + self.snows[i]);
			
			var snow = self.snows[key];
			var wind = self.windMapBySnow[snow.getId()];
			
			if (snow.isTurnOn() == true) {
				wind = this._changeAndGetWind(snow, wind);
				snow.initTurn();
				
				wind.push(snow);
				self.gravity.pull(snow);
			} else {
				wind.push(snow);
				self.gravity.pull(snow);
			}
			
			snow.increaseTurnCount();
			
			if (snow.isExpired()) {
				snow.destroy();
				this._createSnowAndMappingToWind(key);
			}
		}
	};
	
	this._changeAndGetWind = function(snow, wind) {
		var nextWind = this.winds[wind.getId()];
		this.windMapBySnow[snow.getId()] = nextWind;
		
		console.log(wind == nextWind);
		
		return nextWind;
	};
	
	this._initSelf = function() {
		this.windowMaxX = 800;
		this.windowMaxY = 400;
	};
	
	this._initWinds = function() {
		var options1 = {
			id : 1
			, horizontalDirection : 1
			, speed : 3
		};
		
		var options2 = {
			id : 2
			, horizontalDirection : 1
			, speed : 1
		};
		
		var options3 = {
			id : 3
			, horizontalDirection : -1
			, speed : 1
		};
		
		var options4 = {
			id : 4
			, horizontalDirection : 1
			, speed : 1
		};
		
		var wind1 = new Wind(options1);
		var wind2 = new Wind(options2);
		var wind3 = new Wind(options3);
		var wind4 = new Wind(options4);
		
		this.winds[wind1.getId()] = wind2;
		this.winds[wind2.getId()] = wind3;
		this.winds[wind3.getId()] = wind4;
		this.winds[wind4.getId()] = wind1;
		
		var count = 0;
		
		for (var key in this.winds) {
			count++;
		}
		
		this.windCount = count;
	};
	
	this._initGravity = function() {
		var options = {
			gravity : 2
		};
		
		this.gravity = new Gravity(options);
	};
	
	this._initSnows = function() {
		var max = 96;
		
		for (var i = 0; i < max; i++) {
			this._createSnowAndMappingToWind("" + i);
		}
	};
	
	this._createSnowAndMappingToWind = function (id) {
		var randX = this._getRandomX();
		var randY = this._getRandomY();
		var width = this._getWidth();
	//	var height = this._getHeight();
		var height = width;
		
		randY = randY * -1;
		
		var options = {
			applicationContext : this.self
			, parentHtmlObject : this.parentHtmlObject
			, id : id
			, x : randX
			, y : randY
			, width : width
			, height : height
			, maxX : this.windowMaxX
			, maxY : this.windowMaxY
		};
		
		var snow = new Snow(options);
		
		snow.init();
		this.snows[id] = snow;
		
		//start mapping wind and snow. need to devide method
		var windId = Math.floor((Math.random() * this.windCount) + 1);
		this.windMapBySnow[snow.getId()] = this.winds[windId];
	};
	
	this._getRandomX = function() {
		var result = Math.floor(Math.random() * this.windowMaxX);
		
		return result;
	};
	
	this._getRandomY = function() {
		var result = Math.floor(Math.random() * this.windowMaxY);
		
		return result;
	};
	
	this._getWidth = function() {
		var type = [];
		type[0] = 5;
		type[1] = 10;
		type[2] = 15;
		
		var index = (Math.floor(Math.random() * 3));
		
		return type[index];
	};
	
	this._getHeight = function() {
		return 10;
	};
}